package team.seekers.oj.lesson.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.seekers.oj.auth.enums.PermissionEnum;
import team.seekers.oj.common.entity.UserSessionDTO;
import team.seekers.oj.lesson.conventer.ChapterConverter;
import team.seekers.oj.lesson.conventer.ChapterManageConverter;
import team.seekers.oj.lesson.conventer.SectionConverter;
import team.seekers.oj.lesson.conventer.SectionManageConverter;
import team.seekers.oj.lesson.dao.ChapterDao;
import team.seekers.oj.lesson.dao.SectionDao;
import team.seekers.oj.lesson.dto.ChapterCreateReqDTO;
import team.seekers.oj.lesson.dto.ChapterManageDTO;
import team.seekers.oj.lesson.dto.SectionCreateReqDTO;
import team.seekers.oj.lesson.dto.SectionManageDTO;
import team.seekers.oj.lesson.entity.ChapterDO;
import team.seekers.oj.lesson.entity.SectionDO;

@Slf4j
@Service
public class SectionManageService {

    @Autowired
    private SectionDao sectionDao;

    @Autowired
    private SectionConverter sectionConverter;

    @Autowired
    private SectionManageConverter sectionManageConverter;

    public Long create(SectionCreateReqDTO sectionCreateReqDTO){
        SectionDO sectionDO = sectionConverter.from(sectionCreateReqDTO);
        sectionDao.save(sectionDO);
        return sectionDO.getChapterId();
    }

    public void update(SectionManageDTO sectionManageDTO){
        SectionDO sectionDO = sectionManageConverter.from(sectionManageDTO);
        sectionDao.updateById(sectionDO);
    }

    public void delete(Long sectionId){
        sectionDao.removeById(sectionId);
    }

    public SectionManageDTO query(Long sectionId){
        SectionDO sectionDO = sectionDao.getById(sectionId);
        SectionManageDTO sectionManageDTO = sectionManageConverter.to(sectionDO);
        return sectionManageDTO;
    }

    public boolean isSectionManager(Long sectionCreatorId, UserSessionDTO userSessionDTO) {
        return PermissionEnum.SUPERADMIN.in(userSessionDTO) ||
                userSessionDTO.userIdEquals(sectionCreatorId);
    }



}
